<?php

namespace IMT\UserBundle\Form;

use Doctrine\ORM\EntityRepository;

use Symfony\Component\OptionsResolver\OptionsResolverInterface;

use Symfony\Component\Form\FormBuilderInterface;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class ContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', 'text')
            ->add('firstname', 'text', array(
            	'required' => false		
            ))
            ->add('email', 'email')
            ->add('accounts', 'entity', array(
            	'class' => 'IMTAccountBundle:Account',
            	'property' => 'title',
                'multiple'     => true,
                'expanded'     => true,
            	'query_builder' =>  function(EntityRepository $er) use ($options) {
            		return $er->createQueryBuilder('a')
            			->where('a.user = :user')
            			->setParameter('user', $options['user'])
            			->orderBy('a.title', 'ASC');
            	},
                'label'  => 'Comptes',
				'required' => false
            ))
        ;
    }
    
	public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'IMT\UserBundle\Entity\Contact',
        	'user' => null
        ));
    } 

    public function getName()
    {
        return 'imt_userbundle_contacttype';
    }
}
